package com.rxgscd.yy.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.rxgscd.yy.MainActivity;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

	//private static final String APP_ID = "wx15d244c8734a33f0";

	// IWXAPI 是第三方app和微信通信的openapi接口
	private IWXAPI api;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 通过WXAPIFactory工厂，获取IWXAPI的实例
		api = WXAPIFactory.createWXAPI(this, MainActivity.APP_ID, true);
		api.registerApp(MainActivity.APP_ID);

		try {
			api.handleIntent(getIntent(), (IWXAPIEventHandler) this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onReq(BaseReq req) {
		switch (req.getType()) {
		case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:

			break;
		case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:

			break;
		default:
			break;
		}
		finish();
	}

	@Override
	public void onResp(BaseResp resp) {
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK: // 正确返回 登陆成功

			// 获取登录返回的code
			if (resp instanceof SendAuth.Resp) {
				SendAuth.Resp newResp = (SendAuth.Resp) resp;
				// 获取微信传回的code
				String code = newResp.code;
				Log.i("UNITY", "resp wxcode == " + code);
				MainActivity.instance.getAccessToken(code);
			
			}
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:

			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:

			break;
		case BaseResp.ErrCode.ERR_UNSUPPORT:

			break;
		default:

			break;
		}
		finish();
	}
}
